/**
 * Created by cxl on 2017/2/25.
 */
window.onload=function () {
    $.ajax({
        type: 'get',
        url: 'http://192.168.23.81:3000/api/getcategorybyid' + window.location.search,
        dataType: 'json',
        success: function (data) {
            $("#category_name").html(data.result[0].category);
            var pages = 0;
            console.log(data);
            function on_load(pageid) {
                $.ajax({
                    type: 'get',
                    url: 'http://192.168.23.81:3000/api/getproductlist' + window.location.search + "&pageid=" + pageid,
                    dataType: 'json',
                    success: function (data) {
                        console.log(data);
                        var html = template("template", data);
                        $(".main .product ul").html(html);
                        pages = Math.ceil(data.totalCount / data.pagesize)
                        var option = "";
                        for (var i = 0; i < pages; i++) {
                            option += '<option value="' + (i + 1) + '">' + (i + 1) + '/' + pages + '</option>';
                        }
                        $("#select").html(option);
                        $("#select").val(pageid);
                        $(".dianshi li").on("click",function () {
                            window.location.href = "getproduct.html?productid="+$(this).attr("productid");
                        });
                    }
                });
            }

            var pageid = 1;
            on_load(1);
            $("#shang").on("click", function () {
                pageid -= 1;
                if (pageid > 0) {
                    on_load(pageid)
                }
                else {
                    return false
                }
            });
            $("#xia").on("click", function () {
                pageid += 1;
                if (pageid <= pages) {
                    on_load(pageid);
                }
                else {
                    return false;
                }
            })
            $("#select").on("input", function () {
                pageid = $(this).val()
                on_load(pageid)
            });
        }
    });
}